/*
 * TransiCast / Qt
 * (c) 2012 Joachim Pfeiffer
 * Please refer to http://code.google.com/p/transitacst for more information
 */
#include <string>
#include <stdio.h>

#include "busynotify.hpp"
#include <QPixmap>
#include <QPainter>
#include <QDesktopWidget>
#include <QLabel>
#include <QStatusBar>

BusyNotify *BusyNotifier;

static QPixmap *waitPixmap[18];
static QTimer *timer;
static QWidget *widget;
static QLabel *lbl;
static int pixmapIx = 0;

void BusyNotify::init()
{
    BusyNotifier = new BusyNotify(modelParent, view, app);

    //load sand clock image
    QString *filenameS;
    char filename[40];
    for (int i = 0; i < 18; i++) {
        sprintf(filename, ":/bushg_blu%d.png", i * 20);
        filenameS = new QString(filename);
        waitPixmap[i] = new QPixmap(*filenameS, 0, Qt::AutoColor);
    }

    screenWidth = modelParent->width();
    screenHeight = modelParent->height();

    timer = new QTimer();
    timer->setInterval(100);
    timer->setSingleShot(false);

    Qt::WindowFlags flags = Qt::Widget; // ::Dialog;
    flags |= Qt::FramelessWindowHint;
    flags |= Qt::CustomizeWindowHint; // | Qt::WindowTitleHint;
    flags |= Qt::WindowStaysOnTopHint;

    widget = new QWidget(modelParent, flags);
    widget->setVisible(false);
    widget->setAttribute(Qt::WA_TranslucentBackground, false);
    widget->resize(waitPixmap[0]->width() + 2, waitPixmap[0]->height() + 2);
    widget->move(screenWidth / 2 - 24, screenHeight / 2 - widget->height() / 2);

    lbl = new QLabel(widget);
    lbl->setPixmap(*waitPixmap[0]);
    lbl->setAlignment(Qt::AlignCenter);
    lbl->setAttribute(Qt::WA_TranslucentBackground, false);

    connect( timer, SIGNAL(timeout()), BusyNotifier, SLOT(TimerTimeout()) );
}

void BusyNotify::showBusyCursor()
{
    if (!waiting) {
        screenWidth = modelParent->width();
        screenHeight = modelParent->height();
        widget->move(screenWidth / 2 - 24, screenHeight / 2 - widget->height() / 2);

        widget->setVisible(true);

        // this will force the widget to show up
        // as fast as possible
        QApplication::processEvents();

        waiting = true;
        timer->start();
    }
}

void BusyNotify::hideBusyCursor()
{
    if (waiting) {
        widget->setVisible(false);
        timer->stop();
        waiting = false;
    }
}

void BusyNotify::TimerTimeout()
{
    screenWidth = modelParent->width();
    screenHeight = modelParent->height();
    widget->move(screenWidth / 2 - 24, screenHeight / 2 - widget->height() / 2);

    if (!widget->isVisible())
        return;

    pixmapIx++;
    if (pixmapIx >= 18)
        pixmapIx = 0;
    lbl->setPixmap(*waitPixmap[pixmapIx]);

    QTimer::singleShot(0, widget, SLOT(repaint()));
}

BusyNotify::BusyNotify(QMainWindow *parent, QTreeView *parentView, QApplication *parentApp) : QObject()
{
    waiting = false;

    modelParent = parent;
    view = parentView;
    app = parentApp;

}
